package com.wlwx.simulator.message.outbound;

import com.wlwx.simulator.codec.CMPPCommand;
import com.wlwx.simulator.message.basic.BaseOutBoundMessage;

import java.nio.ByteBuffer;

public class DeliverOutBoundRequest extends BaseOutBoundMessage {
    private long   msgId;
    // 21 byte
    private String destId;

    // 10 byte
    private String serviceId;

    private byte   tpPid;

    private byte   tpUdhi;

    private byte   msgFormat = 8;

    // 21 byte
    private String srcTerminalId;

    private byte   registeredDelivery;

    private byte   msgLength;

    private byte[] msgContent;

    // 8 byte
    private String reversed = String.format("%08d", 0);

    public DeliverOutBoundRequest(int sequenceId) {
        super(CMPPCommand.CMPP_DELIVER_REQ_COMMAND, sequenceId);
    }

    @Override
    public byte[] getBody() {
        /**
         * 0：非状态报告
         * 1：状态报告
         */

        ByteBuffer byteBuffer = ByteBuffer.allocate(msgContent.length + 73);
        byteBuffer
                .putLong(msgId)
                .put(destId.getBytes())
                .put(serviceId.getBytes())
                .put(tpPid)
                .put(tpUdhi)
                .put(msgFormat)
                .put(srcTerminalId.getBytes())
                .put(registeredDelivery)
                .put((byte) msgContent.length)
                .put(msgContent)
                .put(reversed.getBytes());

        return byteBuffer.array();
    }

    public long getMsgId() {
        return msgId;
    }

    public void setMsgId(long msgId) {
        this.msgId = msgId;
    }

    public String getDestId() {
        return destId;
    }

    public void setDestId(String destId) {
        this.destId = destId;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public byte getTpPid() {
        return tpPid;
    }

    public void setTpPid(byte tpPid) {
        this.tpPid = tpPid;
    }

    public byte getTpUdhi() {
        return tpUdhi;
    }

    public void setTpUdhi(byte tpUdhi) {
        this.tpUdhi = tpUdhi;
    }

    public byte getMsgFormat() {
        return msgFormat;
    }

    public void setMsgFormat(byte msgFormat) {
        this.msgFormat = msgFormat;
    }

    public String getSrcTerminalId() {
        return srcTerminalId;
    }

    public void setSrcTerminalId(String srcTerminalId) {
        this.srcTerminalId = srcTerminalId;
    }

    public byte getRegisteredDelivery() {
        return registeredDelivery;
    }

    public void setRegisteredDelivery(byte registeredDelivery) {
        this.registeredDelivery = registeredDelivery;
    }

    public byte getMsgLength() {
        return msgLength;
    }

    public void setMsgLength(byte msgLength) {
        this.msgLength = msgLength;
    }

    public byte[] getMsgContent() {
        return msgContent;
    }

    public void setMsgContent(byte[] msgContent) {
        this.msgContent = msgContent;
    }

    public String getReversed() {
        return reversed;
    }

    public void setReversed(String reversed) {
        this.reversed = reversed;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer("CMPP20 DELIVER REQ : [");

        sb.append("msgId")              .append(":").append(msgId                 ).append(", ");
        sb.append("destId")             .append(":").append(destId                ).append(", ");
        sb.append("serviceId")          .append(":").append(serviceId             ).append(", ");
        sb.append("tpPid")              .append(":").append(tpPid                 ).append(", ");
        sb.append("tpUdhi")             .append(":").append(tpUdhi                ).append(", ");
        sb.append("msgFormat")          .append(":").append(msgFormat             ).append(", ");
        sb.append("srcTerminalId")      .append(":").append(srcTerminalId         ).append(", ");
        sb.append("registeredDelivery") .append(":").append(registeredDelivery    ).append(", ");
        sb.append("msgLength")          .append(":").append(msgLength             ).append(", ");
        sb.append("msgContent")         .append(":").append(new String(msgContent)).append(", ");
        sb.append("reversed")           .append(":").append(reversed              );
        sb.append("]");

        return sb.toString();
    }
}
